<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_AlbumViewController extends Engine_Content_Widget_Abstract {

  protected $_childCount;

  public function indexAction() {

    if (!Engine_Api::_()->core()->hasSubject()) {
      return $this->setNoRender();
    }

    $this->view->showLightBox = Engine_Api::_()->sitealbum()->showLightBoxPhoto();
    $settings = Engine_Api::_()->getApi('settings', 'core');

    $this->view->album = $album = Engine_Api::_()->core()->getSubject();

    // Prepare params
    $this->view->page = $page = $this->_getParam('page');

    $this->view->is_ajax = $is_ajax = $this->_getParam('isajax', '');
    $this->view->itemCountPerPage = $this->_getParam('itemCountPerPage', 40);
    $this->view->marginPhoto = $this->_getParam('margin_photo', 12);
    $front = Zend_Controller_Front::getInstance();
    $this->view->comment_view = $comment_view = $front->getRequest()->getParam('comment', 'false');
    $this->view->comment_enable = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.comment.view', 1);
    if ($comment_view == 'false' || empty($this->view->comment_enable)) {
      // Prepare data
      if (!Engine_Api::_()->sitealbum()->isLessThan417AlbumModule()) {
        $photoTable = Engine_Api::_()->getItemTable('album_photo');
        $this->view->paginator = $paginator = $photoTable->getPhotoPaginator(array(
            'album' => $album,
                ));
      } else {
        $this->view->paginator = $paginator = $album->getCollectiblesPaginator();
      }
      $paginator->setItemCountPerPage($this->view->itemCountPerPage);
      $paginator->setCurrentPageNumber($page);
      $this->view->count = $this->_childCount = $paginator->getTotalItemCount();
    } else {
      $this->view->page = $page = $front->getRequest()->getParam('page', 1);

      $this->view->paginator = $paginator = Engine_Api::_()->sitealbum()->getCommentedPhotos($album->getIdentity());
      $paginator->setItemCountPerPage(30);
      $paginator->setCurrentPageNumber($page);
      $this->view->count = $paginator->getTotalItemCount();
    }
    // Do other stuff
    $this->view->mine = true;

    $this->view->canEdit = Engine_Api::_()->authorization()->isAllowed($album, null, 'edit');
    $viewer = Engine_Api::_()->user()->getViewer();
    if (!$album->getOwner()->isSelf($viewer)) {
      $album->getTable()->update(array(
          'view_count' => new Zend_Db_Expr('view_count + 1'),
              ), array(
          'album_id = ?' => $album->getIdentity(),
      ));
      $this->view->mine = false;
    }

    //SET COUNT TO THE TITLE
    $viewer_id = $viewer->getIdentity();
    $this->view->allowView = $this->view->canMakeFeatured = false;
    if (!empty($viewer_id) && $viewer->level_id == 1) {
      $this->view->canMakeFeatured = true;
      $auth = Engine_Api::_()->authorization()->context;
      $this->view->allowView = $auth->isAllowed($album, 'everyone', 'view') === 1 ? true : false ||$auth->isAllowed($album, 'registered', 'view') === 1 ? true : false;
    }
    $this->view->canComment = $canComment = $album->authorization()->isAllowed($viewer, 'comment');
    $countView = Zend_Registry::isRegistered('sitealbum_countview') ? Zend_Registry::get('sitealbum_countview') : null;
    $sitealbumCoreview = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.coreview', null);
    if (empty($sitealbumCoreview) || empty($countView)) {
      return $this->setNoRender();
    }

    // WORK START FOR SUGGESTION
    $suggestionPluginStatus = Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('suggestion');
    if (!empty($suggestionPluginStatus)) {
      $flag = false;
      if (!empty($suggestionPluginStatus)) {
        $linkShouldShow = Engine_Api::_()->suggestion()->getModSettings('album', 'link');

        $SuggVersion = Engine_Api::_()->getDbtable('modules', 'core')->getModule('suggestion')->version;
        $versionStatus = strcasecmp($SuggVersion, '4.1.7p1');
        if ($versionStatus >= 0) {
          $modContentObj = Engine_Api::_()->suggestion()->getSuggestedFriend('album', $album->getIdentity(), 1);
          if (!empty($modContentObj)) {
            $contentCreatePopup = @COUNT($modContentObj);
          }
        }

        if (!empty($linkShouldShow) && !empty($contentCreatePopup)) {
          $flag = true;
        }
      }
      $this->view->isSugg = $flag;
      // END WORK FOR SUGGESTION
    }
  }

  public function getChildCount() {
    return $this->_childCount;
  }

}

?>
